package cn.zl.common.properties;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "zl")
public class ZLProperties {

    private String contextPath;
    private String ip = "";
    private String ffmpeg = "";
    private ZLM zlm = new ZLM();
    private Map<String, String> playUrlTemplate = new HashMap<>();
    private SIP sip = new SIP();

    public String getContextPath() {
        return contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    public String getIp() {
        if (StrUtil.isBlank(ip)) {
            List<String> ips = new ArrayList<>();
            LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(networkInterface -> {
                String name = networkInterface.getName();
                String displayName = networkInterface.getDisplayName();
                // 排除docker 虚拟机 vmware
                return !name.contains("docker")
                        && !name.contains("lo")
                        && !name.toLowerCase(Locale.ROOT).startsWith("vmware")
                        && !displayName.toLowerCase(Locale.ROOT).startsWith("vmware");
            }, address -> !address.isLoopbackAddress() && address instanceof Inet4Address && address.isSiteLocalAddress());
            for (InetAddress address : localAddressList) {
                ips.add(address.getHostAddress());
            }
            if (ips.isEmpty()) {
                return "127.0.0.1";
            }
            if (ips.size() == 1) {
                return ips.getFirst();
            }
            return StrUtil.join(",", ips);
        }
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getFfmpeg() {
        return ffmpeg;
    }

    public void setFfmpeg(String ffmpeg) {
        this.ffmpeg = ffmpeg;
    }

    public ZLM getZlm() {
        return zlm;
    }

    public void setZlm(ZLM zlm) {
        this.zlm = zlm;
    }

    public Map<String, String> getPlayUrlTemplate() {
        return playUrlTemplate;
    }

    public void setPlayUrlTemplate(Map<String, String> playUrlTemplate) {
        this.playUrlTemplate = playUrlTemplate;
    }

    public SIP getSip() {
        return sip;
    }

    public void setSip(SIP sip) {
        this.sip = sip;
    }

    public static class ZLM {
        private int threadNum = 4;
        private int logLevel = 3;
        private String ip = "";
        private int httpPort = 18080;
        private int rtspPort = 10554;
        private int rtmpPort = 11935;
        private boolean rtpEnabled = false;

        public int getThreadNum() {
            return threadNum;
        }

        public void setThreadNum(int threadNum) {
            this.threadNum = threadNum;
        }

        public int getLogLevel() {
            return logLevel;
        }

        public void setLogLevel(int logLevel) {
            this.logLevel = logLevel;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public int getHttpPort() {
            return httpPort;
        }

        public void setHttpPort(int httpPort) {
            this.httpPort = httpPort;
        }

        public int getRtspPort() {
            return rtspPort;
        }

        public void setRtspPort(int rtspPort) {
            this.rtspPort = rtspPort;
        }

        public int getRtmpPort() {
            return rtmpPort;
        }

        public void setRtmpPort(int rtmpPort) {
            this.rtmpPort = rtmpPort;
        }

        public boolean isRtpEnabled() {
            return rtpEnabled;
        }

        public void setRtpEnabled(boolean rtpEnabled) {
            this.rtpEnabled = rtpEnabled;
        }
    }

    public static class SIP {
        private String id;
        private String domain;
        private String ip = "";
        private int port = 15060;
        private String password;
        private boolean alarm;

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        public String getDomain() {
            return domain;
        }

        public void setDomain(String domain) {
            this.domain = domain;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public boolean isAlarm() {
            return alarm;
        }

        public void setAlarm(boolean alarm) {
            this.alarm = alarm;
        }
    }

}
